

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>packnet_sfm.networks.depth.PackNet01 &mdash; PackNet-SfM 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html">
          

          
            
            <img src="../../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../configs/configs.html">Configs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../scripts/scripts.html">Scripts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../trainers/trainers.html">Trainers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../datasets/datasets.html">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../models/models.html">Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../networks/networks.html">Networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../losses/losses.html">Losses</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../loggers/loggers.html">Loggers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../geometry/geometry.html">Geometry</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../utils/utils.html">Utils</a></li>
</ul>
<p class="caption"><span class="caption-text">Contact</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://tri.global">Toyota Research Institute</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/packnet-sfm">PackNet-SfM GitHub</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/DDAD">DDAD GitHub</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">PackNet-SfM</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>packnet_sfm.networks.depth.PackNet01</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for packnet_sfm.networks.depth.PackNet01</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020 Toyota Research Institute.  All rights reserved.</span>

<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.networks.layers.packnet.layers01</span> <span class="kn">import</span> \
    <span class="n">PackLayerConv3d</span><span class="p">,</span> <span class="n">UnpackLayerConv3d</span><span class="p">,</span> <span class="n">Conv2D</span><span class="p">,</span> <span class="n">ResidualBlock</span><span class="p">,</span> <span class="n">InvDepth</span>

<div class="viewcode-block" id="PackNet01"><a class="viewcode-back" href="../../../../networks/depth/networks.depth.PackNet01.html#packnet_sfm.networks.depth.PackNet01.PackNet01">[docs]</a><span class="k">class</span> <span class="nc">PackNet01</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    PackNet network with 3d convolutions (version 01, from the CVPR paper).</span>

<span class="sd">    https://arxiv.org/abs/1905.02693</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    dropout : float</span>
<span class="sd">        Dropout value to use</span>
<span class="sd">    version : str</span>
<span class="sd">        Has a XY format, where:</span>
<span class="sd">        X controls upsampling variations (not used at the moment).</span>
<span class="sd">        Y controls feature stacking (A for concatenation and B for addition)</span>
<span class="sd">    kwargs : dict</span>
<span class="sd">        Extra parameters</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dropout</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">=</span> <span class="n">version</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="c1"># Input/output channels</span>
        <span class="n">in_channels</span> <span class="o">=</span> <span class="mi">3</span>
        <span class="n">out_channels</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="c1"># Hyper-parameters</span>
        <span class="n">ni</span><span class="p">,</span> <span class="n">no</span> <span class="o">=</span> <span class="mi">64</span><span class="p">,</span> <span class="n">out_channels</span>
        <span class="n">n1</span><span class="p">,</span> <span class="n">n2</span><span class="p">,</span> <span class="n">n3</span><span class="p">,</span> <span class="n">n4</span><span class="p">,</span> <span class="n">n5</span> <span class="o">=</span> <span class="mi">64</span><span class="p">,</span> <span class="mi">64</span><span class="p">,</span> <span class="mi">128</span><span class="p">,</span> <span class="mi">256</span><span class="p">,</span> <span class="mi">512</span>
        <span class="n">num_blocks</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
        <span class="n">pack_kernel</span> <span class="o">=</span> <span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
        <span class="n">unpack_kernel</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
        <span class="n">iconv_kernel</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
        <span class="c1"># Initial convolutional layer</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pre_calc</span> <span class="o">=</span> <span class="n">Conv2D</span><span class="p">(</span><span class="n">in_channels</span><span class="p">,</span> <span class="n">ni</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="c1"># Support for different versions</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">==</span> <span class="s1">&#39;A&#39;</span><span class="p">:</span>  <span class="c1"># Channel concatenation</span>
            <span class="n">n1o</span><span class="p">,</span> <span class="n">n1i</span> <span class="o">=</span> <span class="n">n1</span><span class="p">,</span> <span class="n">n1</span> <span class="o">+</span> <span class="n">ni</span> <span class="o">+</span> <span class="n">no</span>
            <span class="n">n2o</span><span class="p">,</span> <span class="n">n2i</span> <span class="o">=</span> <span class="n">n2</span><span class="p">,</span> <span class="n">n2</span> <span class="o">+</span> <span class="n">n1</span> <span class="o">+</span> <span class="n">no</span>
            <span class="n">n3o</span><span class="p">,</span> <span class="n">n3i</span> <span class="o">=</span> <span class="n">n3</span><span class="p">,</span> <span class="n">n3</span> <span class="o">+</span> <span class="n">n2</span> <span class="o">+</span> <span class="n">no</span>
            <span class="n">n4o</span><span class="p">,</span> <span class="n">n4i</span> <span class="o">=</span> <span class="n">n4</span><span class="p">,</span> <span class="n">n4</span> <span class="o">+</span> <span class="n">n3</span>
            <span class="n">n5o</span><span class="p">,</span> <span class="n">n5i</span> <span class="o">=</span> <span class="n">n5</span><span class="p">,</span> <span class="n">n5</span> <span class="o">+</span> <span class="n">n4</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">==</span> <span class="s1">&#39;B&#39;</span><span class="p">:</span>  <span class="c1"># Channel addition</span>
            <span class="n">n1o</span><span class="p">,</span> <span class="n">n1i</span> <span class="o">=</span> <span class="n">n1</span><span class="p">,</span> <span class="n">n1</span> <span class="o">+</span> <span class="n">no</span>
            <span class="n">n2o</span><span class="p">,</span> <span class="n">n2i</span> <span class="o">=</span> <span class="n">n2</span><span class="p">,</span> <span class="n">n2</span> <span class="o">+</span> <span class="n">no</span>
            <span class="n">n3o</span><span class="p">,</span> <span class="n">n3i</span> <span class="o">=</span> <span class="n">n3</span><span class="o">//</span><span class="mi">2</span><span class="p">,</span> <span class="n">n3</span><span class="o">//</span><span class="mi">2</span> <span class="o">+</span> <span class="n">no</span>
            <span class="n">n4o</span><span class="p">,</span> <span class="n">n4i</span> <span class="o">=</span> <span class="n">n4</span><span class="o">//</span><span class="mi">2</span><span class="p">,</span> <span class="n">n4</span><span class="o">//</span><span class="mi">2</span>
            <span class="n">n5o</span><span class="p">,</span> <span class="n">n5i</span> <span class="o">=</span> <span class="n">n5</span><span class="o">//</span><span class="mi">2</span><span class="p">,</span> <span class="n">n5</span><span class="o">//</span><span class="mi">2</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown PackNet version </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">version</span><span class="p">))</span>

        <span class="c1"># Encoder</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">pack1</span> <span class="o">=</span> <span class="n">PackLayerConv3d</span><span class="p">(</span><span class="n">n1</span><span class="p">,</span> <span class="n">pack_kernel</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pack2</span> <span class="o">=</span> <span class="n">PackLayerConv3d</span><span class="p">(</span><span class="n">n2</span><span class="p">,</span> <span class="n">pack_kernel</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pack3</span> <span class="o">=</span> <span class="n">PackLayerConv3d</span><span class="p">(</span><span class="n">n3</span><span class="p">,</span> <span class="n">pack_kernel</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pack4</span> <span class="o">=</span> <span class="n">PackLayerConv3d</span><span class="p">(</span><span class="n">n4</span><span class="p">,</span> <span class="n">pack_kernel</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pack5</span> <span class="o">=</span> <span class="n">PackLayerConv3d</span><span class="p">(</span><span class="n">n5</span><span class="p">,</span> <span class="n">pack_kernel</span><span class="p">[</span><span class="mi">4</span><span class="p">])</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">conv1</span> <span class="o">=</span> <span class="n">Conv2D</span><span class="p">(</span><span class="n">ni</span><span class="p">,</span> <span class="n">n1</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv2</span> <span class="o">=</span> <span class="n">ResidualBlock</span><span class="p">(</span><span class="n">n1</span><span class="p">,</span> <span class="n">n2</span><span class="p">,</span> <span class="n">num_blocks</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">,</span> <span class="n">dropout</span><span class="o">=</span><span class="n">dropout</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv3</span> <span class="o">=</span> <span class="n">ResidualBlock</span><span class="p">(</span><span class="n">n2</span><span class="p">,</span> <span class="n">n3</span><span class="p">,</span> <span class="n">num_blocks</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="mi">1</span><span class="p">,</span> <span class="n">dropout</span><span class="o">=</span><span class="n">dropout</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv4</span> <span class="o">=</span> <span class="n">ResidualBlock</span><span class="p">(</span><span class="n">n3</span><span class="p">,</span> <span class="n">n4</span><span class="p">,</span> <span class="n">num_blocks</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="mi">1</span><span class="p">,</span> <span class="n">dropout</span><span class="o">=</span><span class="n">dropout</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">conv5</span> <span class="o">=</span> <span class="n">ResidualBlock</span><span class="p">(</span><span class="n">n4</span><span class="p">,</span> <span class="n">n5</span><span class="p">,</span> <span class="n">num_blocks</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="mi">1</span><span class="p">,</span> <span class="n">dropout</span><span class="o">=</span><span class="n">dropout</span><span class="p">)</span>

        <span class="c1"># Decoder</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">unpack5</span> <span class="o">=</span> <span class="n">UnpackLayerConv3d</span><span class="p">(</span><span class="n">n5</span><span class="p">,</span> <span class="n">n5o</span><span class="p">,</span> <span class="n">unpack_kernel</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unpack4</span> <span class="o">=</span> <span class="n">UnpackLayerConv3d</span><span class="p">(</span><span class="n">n5</span><span class="p">,</span> <span class="n">n4o</span><span class="p">,</span> <span class="n">unpack_kernel</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unpack3</span> <span class="o">=</span> <span class="n">UnpackLayerConv3d</span><span class="p">(</span><span class="n">n4</span><span class="p">,</span> <span class="n">n3o</span><span class="p">,</span> <span class="n">unpack_kernel</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unpack2</span> <span class="o">=</span> <span class="n">UnpackLayerConv3d</span><span class="p">(</span><span class="n">n3</span><span class="p">,</span> <span class="n">n2o</span><span class="p">,</span> <span class="n">unpack_kernel</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unpack1</span> <span class="o">=</span> <span class="n">UnpackLayerConv3d</span><span class="p">(</span><span class="n">n2</span><span class="p">,</span> <span class="n">n1o</span><span class="p">,</span> <span class="n">unpack_kernel</span><span class="p">[</span><span class="mi">4</span><span class="p">])</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">iconv5</span> <span class="o">=</span> <span class="n">Conv2D</span><span class="p">(</span><span class="n">n5i</span><span class="p">,</span> <span class="n">n5</span><span class="p">,</span> <span class="n">iconv_kernel</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iconv4</span> <span class="o">=</span> <span class="n">Conv2D</span><span class="p">(</span><span class="n">n4i</span><span class="p">,</span> <span class="n">n4</span><span class="p">,</span> <span class="n">iconv_kernel</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iconv3</span> <span class="o">=</span> <span class="n">Conv2D</span><span class="p">(</span><span class="n">n3i</span><span class="p">,</span> <span class="n">n3</span><span class="p">,</span> <span class="n">iconv_kernel</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iconv2</span> <span class="o">=</span> <span class="n">Conv2D</span><span class="p">(</span><span class="n">n2i</span><span class="p">,</span> <span class="n">n2</span><span class="p">,</span> <span class="n">iconv_kernel</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iconv1</span> <span class="o">=</span> <span class="n">Conv2D</span><span class="p">(</span><span class="n">n1i</span><span class="p">,</span> <span class="n">n1</span><span class="p">,</span> <span class="n">iconv_kernel</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="mi">1</span><span class="p">)</span>

        <span class="c1"># Depth Layers</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">unpack_disps</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">PixelShuffle</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unpack_disp4</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Upsample</span><span class="p">(</span><span class="n">scale_factor</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;nearest&#39;</span><span class="p">,</span> <span class="n">align_corners</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unpack_disp3</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Upsample</span><span class="p">(</span><span class="n">scale_factor</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;nearest&#39;</span><span class="p">,</span> <span class="n">align_corners</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unpack_disp2</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Upsample</span><span class="p">(</span><span class="n">scale_factor</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;nearest&#39;</span><span class="p">,</span> <span class="n">align_corners</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">disp4_layer</span> <span class="o">=</span> <span class="n">InvDepth</span><span class="p">(</span><span class="n">n4</span><span class="p">,</span> <span class="n">out_channels</span><span class="o">=</span><span class="n">out_channels</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disp3_layer</span> <span class="o">=</span> <span class="n">InvDepth</span><span class="p">(</span><span class="n">n3</span><span class="p">,</span> <span class="n">out_channels</span><span class="o">=</span><span class="n">out_channels</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disp2_layer</span> <span class="o">=</span> <span class="n">InvDepth</span><span class="p">(</span><span class="n">n2</span><span class="p">,</span> <span class="n">out_channels</span><span class="o">=</span><span class="n">out_channels</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disp1_layer</span> <span class="o">=</span> <span class="n">InvDepth</span><span class="p">(</span><span class="n">n1</span><span class="p">,</span> <span class="n">out_channels</span><span class="o">=</span><span class="n">out_channels</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">init_weights</span><span class="p">()</span>

<div class="viewcode-block" id="PackNet01.init_weights"><a class="viewcode-back" href="../../../../networks/depth/networks.depth.PackNet01.html#packnet_sfm.networks.depth.PackNet01.PackNet01.init_weights">[docs]</a>    <span class="k">def</span> <span class="nf">init_weights</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initializes network weights.&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">modules</span><span class="p">():</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Conv2d</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv3d</span><span class="p">)):</span>
                <span class="n">nn</span><span class="o">.</span><span class="n">init</span><span class="o">.</span><span class="n">xavier_uniform_</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">weight</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">m</span><span class="o">.</span><span class="n">bias</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">m</span><span class="o">.</span><span class="n">bias</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">zero_</span><span class="p">()</span></div>

<div class="viewcode-block" id="PackNet01.forward"><a class="viewcode-back" href="../../../../networks/depth/networks.depth.PackNet01.html#packnet_sfm.networks.depth.PackNet01.PackNet01.forward">[docs]</a>    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Runs the network and returns inverse depth maps</span>
<span class="sd">        (4 scales if training and 1 if not).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pre_calc</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>

        <span class="c1"># Encoder</span>

        <span class="n">x1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">conv1</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="n">x1p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pack1</span><span class="p">(</span><span class="n">x1</span><span class="p">)</span>
        <span class="n">x2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">conv2</span><span class="p">(</span><span class="n">x1p</span><span class="p">)</span>
        <span class="n">x2p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pack2</span><span class="p">(</span><span class="n">x2</span><span class="p">)</span>
        <span class="n">x3</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">conv3</span><span class="p">(</span><span class="n">x2p</span><span class="p">)</span>
        <span class="n">x3p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pack3</span><span class="p">(</span><span class="n">x3</span><span class="p">)</span>
        <span class="n">x4</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">conv4</span><span class="p">(</span><span class="n">x3p</span><span class="p">)</span>
        <span class="n">x4p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pack4</span><span class="p">(</span><span class="n">x4</span><span class="p">)</span>
        <span class="n">x5</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">conv5</span><span class="p">(</span><span class="n">x4p</span><span class="p">)</span>
        <span class="n">x5p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pack5</span><span class="p">(</span><span class="n">x5</span><span class="p">)</span>

        <span class="c1"># Skips</span>

        <span class="n">skip1</span> <span class="o">=</span> <span class="n">x</span>
        <span class="n">skip2</span> <span class="o">=</span> <span class="n">x1p</span>
        <span class="n">skip3</span> <span class="o">=</span> <span class="n">x2p</span>
        <span class="n">skip4</span> <span class="o">=</span> <span class="n">x3p</span>
        <span class="n">skip5</span> <span class="o">=</span> <span class="n">x4p</span>

        <span class="c1"># Decoder</span>

        <span class="n">unpack5</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">unpack5</span><span class="p">(</span><span class="n">x5p</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">==</span> <span class="s1">&#39;A&#39;</span><span class="p">:</span>
            <span class="n">concat5</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">unpack5</span><span class="p">,</span> <span class="n">skip5</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">concat5</span> <span class="o">=</span> <span class="n">unpack5</span> <span class="o">+</span> <span class="n">skip5</span>
        <span class="n">iconv5</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">iconv5</span><span class="p">(</span><span class="n">concat5</span><span class="p">)</span>

        <span class="n">unpack4</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">unpack4</span><span class="p">(</span><span class="n">iconv5</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">==</span> <span class="s1">&#39;A&#39;</span><span class="p">:</span>
            <span class="n">concat4</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">unpack4</span><span class="p">,</span> <span class="n">skip4</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">concat4</span> <span class="o">=</span> <span class="n">unpack4</span> <span class="o">+</span> <span class="n">skip4</span>
        <span class="n">iconv4</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">iconv4</span><span class="p">(</span><span class="n">concat4</span><span class="p">)</span>
        <span class="n">disp4</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">disp4_layer</span><span class="p">(</span><span class="n">iconv4</span><span class="p">)</span>
        <span class="n">udisp4</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">unpack_disp4</span><span class="p">(</span><span class="n">disp4</span><span class="p">)</span>

        <span class="n">unpack3</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">unpack3</span><span class="p">(</span><span class="n">iconv4</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">==</span> <span class="s1">&#39;A&#39;</span><span class="p">:</span>
            <span class="n">concat3</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">unpack3</span><span class="p">,</span> <span class="n">skip3</span><span class="p">,</span> <span class="n">udisp4</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">concat3</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">unpack3</span> <span class="o">+</span> <span class="n">skip3</span><span class="p">,</span> <span class="n">udisp4</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">iconv3</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">iconv3</span><span class="p">(</span><span class="n">concat3</span><span class="p">)</span>
        <span class="n">disp3</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">disp3_layer</span><span class="p">(</span><span class="n">iconv3</span><span class="p">)</span>
        <span class="n">udisp3</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">unpack_disp3</span><span class="p">(</span><span class="n">disp3</span><span class="p">)</span>

        <span class="n">unpack2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">unpack2</span><span class="p">(</span><span class="n">iconv3</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">==</span> <span class="s1">&#39;A&#39;</span><span class="p">:</span>
            <span class="n">concat2</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">unpack2</span><span class="p">,</span> <span class="n">skip2</span><span class="p">,</span> <span class="n">udisp3</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">concat2</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">unpack2</span> <span class="o">+</span> <span class="n">skip2</span><span class="p">,</span> <span class="n">udisp3</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">iconv2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">iconv2</span><span class="p">(</span><span class="n">concat2</span><span class="p">)</span>
        <span class="n">disp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">disp2_layer</span><span class="p">(</span><span class="n">iconv2</span><span class="p">)</span>
        <span class="n">udisp2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">unpack_disp2</span><span class="p">(</span><span class="n">disp2</span><span class="p">)</span>

        <span class="n">unpack1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">unpack1</span><span class="p">(</span><span class="n">iconv2</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">==</span> <span class="s1">&#39;A&#39;</span><span class="p">:</span>
            <span class="n">concat1</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">unpack1</span><span class="p">,</span> <span class="n">skip1</span><span class="p">,</span> <span class="n">udisp2</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">concat1</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">unpack1</span> <span class="o">+</span>  <span class="n">skip1</span><span class="p">,</span> <span class="n">udisp2</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">iconv1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">iconv1</span><span class="p">(</span><span class="n">concat1</span><span class="p">)</span>
        <span class="n">disp1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">disp1_layer</span><span class="p">(</span><span class="n">iconv1</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">training</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">disp1</span><span class="p">,</span> <span class="n">disp2</span><span class="p">,</span> <span class="n">disp3</span><span class="p">,</span> <span class="n">disp4</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">disp1</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Toyota Research Institute (TRI)

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>